from rest_framework.views import APIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from datetime import date, timedelta

from users.models import User
from goods.models import GoodsVisitCount
from meiduo_admin.serializers.statistical import UserGoodsCountSerializer

class GoodsVisitCountView(APIView):
    """日分类商品访问量统计"""
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当日日期
        now_date = date.today()
        # 获取当天分类商品访问数量(应该是当天被访问的商品把)
        goods = GoodsVisitCount.objects.filter(date__gte=now_date)

        ser = UserGoodsCountSerializer(goods, many=True)

        # 返回结果
        return Response(ser.data)


class UserMonthCountView(APIView):
    """月增用户统计"""
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当日日期
        now_date = date.today()

        # 30天前
        begin_date = now_date - timedelta(days=29)

        data_list = []
        # 遍历统计
        for i in range(30):
            # 起始日期
            index_date = begin_date + timedelta(days=i)
            # 起始日期下一天
            next_date = begin_date + timedelta(days=i+1)
            count = User.objects.filter(date_joined__gte=index_date, date_joined__lt=next_date).count()
            data_list.append({
                'count': count,
                'date': index_date
            })

        return Response(data_list)


class OrderUserView(APIView):
    """当日的下单用户统计"""
    permission_classes = [IsAdminUser]

    def get(self, request):

        # 当天日期
        now_date = date.today()

        # 当天下单用户总量
        # 这是今天下的总订单数
        # count = len(set(OrderInfo.objects.filter(create_time__gte=now_date)))

        count = len(set(User.objects.filter(orders__create_time__gte=now_date)))

        # 返回结果
        return Response({
            'date': now_date,
            'count': count
        })


class UserDayActiveCountView(APIView):
    """
    日活用户统计
    """
    permission_classes = [IsAdminUser]
    def get(self, request):
        # 获取当天的日期
        now_date = date.today()

        # 获取当天活跃的用户总量
        count = User.objects.filter(last_login__gte=now_date).count()

        # 返回结果
        return Response({
            'date': now_date,
            'count': count
        })


class UserDayCountView(APIView):
    """
    用户日增量的统计
    """
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天的日期
        now_date = date.today()

        # 获取当天注册用户总量
        count = User.objects.filter(date_joined__gte=now_date).count()

        # 返回结果
        return Response({
            'date': now_date,
            'count': count
        })


class UserCountView(APIView):
    """
    用户总量统计
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天日期
        now_date = date.today()

        # 获取用户总量
        count = User.objects.all().count()

        # 返回结果（日期和总量）
        return Response({
            'date': now_date,
            'count': count
        })
